﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Medicae_Toolkit
{
    /// <summary>
    /// Form where the Character object is created and modified by end user.
    /// </summary>
    public partial class CharEditor : Form
    {
        Character Me;
        Character Orig;
        Party PForm;
        bool isnew;

        public CharEditor(Party p, Character c, bool New)
        {
            InitializeComponent();
            Orig = c;
            Me = c;
            PForm = p;
            isnew = New;
            if (!New)
            {
                nameBox.Text = c.name;
                toughBox.Text = c.toughness.ToString();
                woundsBox.Text = c.woundcap.ToString();
                hard.Checked = c.hardy;
                autosang.Checked = c.autosanguinate;
            }
        }

        /// <summary>
        /// Once the user is done, whether or not they did anything, update the Party form
        /// and bring it to the front.
        /// </summary>
        private void End(object sender, EventArgs e)
        {
            PForm.UpdateList();
            PForm.BringToFront();
            Close();
        }

        /// <summary>
        /// Checks all relevant text boxes to make sure all Data has been entered
        /// and then updates the CurrentParty list with the new or changed Character object.
        /// </summary>
        private void saveButton_Click(object sender, EventArgs e)
        {
            if (nameBox.Text == "")
            {
                MessageBox.Show("Please enter a Name!");
                return;
            }
            if (woundsBox.Text == "")
            {
                MessageBox.Show("Please enter the amount of Wounds!");
                return;
            }
            if (toughBox.Text == "")
            {
                MessageBox.Show("Please enter the Toughness Stat!");
                return;
            }
            
            if (isnew)
            {
                PForm.PForm.CurrentParty.Add(Me);
            }
            else
            {
                PForm.PForm.CurrentParty.Remove(Orig);
                PForm.PForm.CurrentParty.Add(Me);
            }
            End(sender, e);
        }

        /// <summary>
        /// As the user types out the Character's name, it is updated in the object.
        /// </summary>
        private void nameBox_TextChanged(object sender, EventArgs e)
        {
            Me.name = nameBox.Text;
        }

        /// <summary>
        /// As the user types out the Character's wounds, it is updated in the object.
        /// </summary>
        private void woundsBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                Me.woundcap = Me.wounds = Convert.ToInt32(woundsBox.Text);
            }
            catch (FormatException fe)
            {
                return; // To keep toolkit from freaking out when accidentally typing chars
            }
        }

        /// <summary>
        /// As the user types out the Character's toughness, it is updated in the object.
        /// </summary>
        private void toughBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                Me.toughness = Convert.ToInt32(toughBox.Text);
            }
            catch (FormatException fe)
            {
                return; // To keep toolkit from freaking out when accidentally typing chars
            }
        }

        private void hard_CheckedChanged(object sender, EventArgs e)
        {
            Me.hardy = hard.Checked;
        }

        private void autosang_CheckedChanged(object sender, EventArgs e)
        {
            Me.autosanguinate = autosang.Checked;
        }
    }
}
